package com.hengpeng.itfinoss.controller.acm;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.web.tags.ListTag.Pagination;
import com.hengpeng.itfinbase.persist.AppGroup;
import com.hengpeng.itfinoss.controller.BaseController;
import com.hengpeng.itfinoss.controller.util.PageHandlerUtil;
import com.hengpeng.itfinoss.service.ServiceFactory;

/**
 * <pre>
 * 系统组管理
 * </pre>
 * @author CHARLES
 * @version 1.0, 2010-8-10
 */
@Controller
@RequestMapping("/acm/appgroup")
public class AppGroupController extends BaseController
{
	@Autowired(required = false)
	protected ServiceFactory serviceFactory;

	/**
	 * <pre>
	 * 查询所有系统组
	 * </pre>
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list(ModelMap map, HttpServletRequest request)
	{
		Pagination page = new Pagination(request);
		page.setPageSize(this.getPageSize(request));
		try
		{
			DynamicQuery query = new DynamicQuery(AppGroup.class);
			query.setIsSearchTotalCount(true);
			query.setPageIndex(page.currentPage);
			query.setPageSize(page.pageSize);
			QueryResult<AppGroup> result = serviceFactory.getPersistService().findWithPage(AppGroup.class, query);
			page.setEntityCount((int) result.getTotalCount());
			map.put("page", page);
			map.put("list", result.getResults());
			return "acm/appGroupList";

		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			return PageHandlerUtil.returnError(map, ex);
		}
	}

	/**
	 * <pre>
	 * 加载单个组
	 * </pre>
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/load/{id}")
	public String load(@PathVariable Long id, ModelMap map)
	{
		if (PageHandlerUtil.validateObjectsNullOrEmpty(id))
		{
			return PageHandlerUtil.returnError(map, "数据没有通过验证，请刷新页面后重试！");
		}
		try
		{
			Set<AppGroup> list = serviceFactory.getAppUserService().findAllGroupSort(id);
			map.put("sortlist", list);
			AppGroup group = serviceFactory.getPersistService().get(AppGroup.class, id);
			map.put("group", group);
			return "acm/appGroupModify";
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			return PageHandlerUtil.returnError(map, ex);
		}
	}

	/**
	 * <pre>
	 * 批量删除系统组
	 * </pre>
	 * @param map
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/remove")
	public String remove(Long[] ids, ModelMap map, HttpSession session)
	{
		if (PageHandlerUtil.validateArrayNullOrEmpty(ids))
		{
			return PageHandlerUtil.returnError(map, "数据没有通过验证，请刷新页面后重试！");
		}
		List<AppGroup> list = serviceFactory.getAppUserService().findChildByIds(ids);
		if (list.size() > 0)
		{
			return PageHandlerUtil.returnError(map, "您删除的组不能存在子级组，请选择最小级别组删除！");
		}

		List<AppGroup> __list = serviceFactory.getAppUserService().findGroupByIds(ids);
		for (AppGroup group : __list)
		{
			if (group.getUsers() != null && group.getUsers().size() > 0)
			{
				return PageHandlerUtil.returnError(map, "您删除的组中存在关联用户，不能删除！");
			}
		}
		try
		{
			Set<Object> set = new HashSet<Object>(Arrays.asList(ids));
			serviceFactory.getAppUserService().deleteGroup(set);
			return PageHandlerUtil.returnSuccess(map, "/acm/appgroup/list");
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			return PageHandlerUtil.returnError(map, ex);
		}
	}

	@RequestMapping(value = "/create")
	public String readyCreate(ModelMap map)
	{
		AppGroup role = new AppGroup();
		map.put("appgroup", role);
		Set<AppGroup> list = serviceFactory.getAppUserService().findAllGroupSort(null);
		map.put("sortlist", list);
		return "acm/appGroupCreate";
	}

	/**
	 * <pre>
	 * 添加系统组
	 * </pre>
	 * @param name 组名称
	 * @param description 描述
	 * @param parentId 所在组
	 * @param session
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/save")
	public String save(String name, String description, Long parent, String extValue,
			HttpSession session, ModelMap map)
	{
		if (PageHandlerUtil.validateObjectsNullOrEmpty(name, parent))
		{
			return PageHandlerUtil.returnError(map, "数据没有通过验证，请刷新页面后重试！");
		}
		
		AppGroup group = null;
		if (parent.equals(0l))
		{
			group = null;
		}
		if (parent != null && !parent.equals(0l))
		{
			group = serviceFactory.getPersistService().get(AppGroup.class, parent);
			if (group == null)
			{
				return PageHandlerUtil.returnError(map, "该分组所在父分组已经不存在，请刷新页面后重试！");
			}
		}
		try
		{
			serviceFactory.getAppUserService().saveGroup(name, description, group, extValue);
			return PageHandlerUtil.returnSuccess(map, "/acm/appgroup/list");
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			return PageHandlerUtil.returnError(map, ex);
		}
	}

	@RequestMapping(value = "/modify")
	public String modify(Long id, String name, String description, Long parent, String extValue, 
			HttpSession session, ModelMap map)
	{
		if (PageHandlerUtil.validateObjectsNullOrEmpty(id, name, parent))
		{
			return PageHandlerUtil.returnError(map, "数据没有通过验证，请刷新页面后重试！");
		}
		AppGroup group = null;

		if (parent.equals(0l))
		{
			group = null;
		}
		if (parent != null && !parent.equals(0l))
		{
			group = serviceFactory.getPersistService().get(AppGroup.class, parent);
			if (group == null)
			{
				return PageHandlerUtil.returnError(map, "该分组所在父分组已经不存在，请刷新页面后重试！");
			}
		}
		try
		{
			serviceFactory.getAppUserService().modifyGroup(id, name, description, group, extValue);
			return PageHandlerUtil.returnSuccess(map, "/acm/appgroup/list");
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			return PageHandlerUtil.returnError(map, ex);
		}
	}

}
